/* GUI CONVERTED from rqcmt.p (converter v1.75) Thu May 17 22:21:07 2001 */
/* rqcmt.p - Category Maintenance                                           */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.             */
/* $Revision: 1.12 $                                                         */
/*V8:ConvertMode=Maintenance                                                */
/* REVISION: 8.5      LAST MODIFIED: 04/15/97   BY: *J1Q2* Patrick Rowan    */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane        */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan       */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan       */
/* REVISION: 8.6E     LAST MODIFIED: 01/29/99   BY: *J392* Steve Nugent     */
/* REVISION: 9.1      LAST MODIFIED: 10/01/99   BY: *N014* Jeff Wootton     */
/* REVISION: 9.1      LAST MODIFIED: 01/19/00   BY: *N077* Vijaya Pakala    */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 06/28/00   BY: *N0DV* Inna Lyubarsky   */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KP* myb              */
/* Old ECO marker removed, but no ECO header exists *F0PN*                  */
/* $Revision: 1.12 $    BY: Rajesh Lokre    DATE: 05/16/01 ECO: *N0YK*          */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/*!
 ----------------------------------------------------------------------------
 DESCRIPTION: ADDS/MODS/DELETES account categories.
              Supports the multi-line Purchase Requisition Module of MFG/PRO.

 Notes:
 1) Account From is a required entry.  Account To is an optional entry.
    If Account To = "", this implies an open-ended range.

 2) Sub-Account From is a required entry, although a blank sub-account is
    acceptable since it is a valid sub-account.
    Sub-Account To is an optional entry.
    If Sub-Account To = "", it implies an open-ended range.
 ============================================================================
!*/

/* DISPLAY TITLE  */
{mfdtitle.i "b+ "}

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE rqcmt_p_2 "From!Account"
/* MaxLen: Comment: */

&SCOPED-DEFINE rqcmt_p_3 "From!Sub-Acct"
/* MaxLen: Comment: */

&SCOPED-DEFINE rqcmt_p_4 "To  !Account"
/* MaxLen: Comment: */

&SCOPED-DEFINE rqcmt_p_5 "To!Sub-Acct"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

/* CONSTANTS */
{rqconst.i}

/* VARIABLES */
define variable var_acct_from_desc    like ac_desc no-undo
   format "x(20)".
define variable var_acct_to_desc      like ac_desc no-undo
   format "x(20)".
define variable var_acct_from         like glt_acct no-undo.
define variable var_sub_from          like glt_sub  no-undo.
define variable var_acct_to           like glt_acct no-undo.
define variable var_sub_to            like glt_sub  no-undo.
define variable work_rqcd_acct_from   like rqcd_acct_from no-undo.
define variable work_rqcd_sub_from    like rqcd_sub_from no-undo.
define variable work_rqcd_acct_to     like rqcd_acct_to no-undo.
define variable work_rqcd_sub_to      like rqcd_sub_to no-undo.
define variable rqcd_det_cntr         as integer no-undo.

/* LOGICALS  */
define variable del-yn                like mfc_logical no-undo
   initial no.
define variable valid_acct            like mfc_logical no-undo
   initial no.
define variable acct_overlap          like mfc_logical no-undo
   initial no.

/* BUFFERS  */
define buffer work_rqcd_det           for rqcd_det.

/* FRAME A: SELECTION FORM   */

/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
rqc_category          colon 30
   rqc_desc              colon 30
 SKIP(.4)  /*GUI*/
with frame a side-labels width 80 attr-space NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-a-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:HIDDEN in frame a = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5.  /*GUI*/

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

/* FRAME B: DETAIL FORM   */
FORM /*GUI*/ 
   rqcd_acct_from column-label {&rqcmt_p_2}
   rqcd_sub_from  column-label {&rqcmt_p_3}
   var_acct_from_desc
   rqcd_acct_to   column-label {&rqcmt_p_4}
   rqcd_sub_to    column-label {&rqcmt_p_5}
   var_acct_to_desc
   with frame b 7 down title color normal
   (getFrameTitle("ACCOUNT_RANGES",21)) width 80 THREE-D /*GUI*/.


/* SET EXTERNAL LABELS */
setFrameLabels(frame b:handle).

/* FRAME C: DETAIL FORM   */
FORM /*GUI*/ 
         skip(.2)    /* ALLOW FOR GUI LOOKUP ICON */
   rqcd_acct_from     no-label
   rqcd_sub_from      no-label
   var_acct_from_desc no-label
   rqcd_acct_to       no-label
   rqcd_sub_to        no-label
   var_acct_to_desc   no-label
   with frame c width 80 THREE-D /*GUI*/.


/* DISPLAY */
view frame a.
hide frame b.
hide frame c.

mainloop:
repeat with frame a:
/*GUI*/ if global-beam-me-up then undo, leave.


   /* PROMPT-FOR KEYS */
   prompt-for
      rqc_category
      with frame a
   editing:

      /* FIND NEXT/PREVIOUS RECORD */
      {mfnp.i rqc_mstr rqc_category rqc_category
         rqc_category rqc_category rqc_category}

      if recno <> ?
      then
         display
            rqc_category
            rqc_desc
         with frame a.

   end.  /* prompt-for */

   /*  ADD/MODIFY/DELETE  */

   /*  VALIDATE KEY  */
   if input rqc_category = ''
   then do:
      {pxmsg.i &MSGNUM=40 &ERRORLEVEL=3} /*BLANK NOT ALLOWED*/
      undo, retry.
   end.

   find rqc_mstr where rqc_category = input rqc_category
      exclusive-lock no-error.

   if not available rqc_mstr
   then do:

      /* ADD NEW RECORD */
      {pxmsg.i &MSGNUM=1 &ERRORLEVEL=1}
      create rqc_mstr.
      rqc_category = input rqc_category.

      if recid(rqc_mstr) = -1 then .

   end.  /* if not available rqc_mstr */

   ststatus = stline[2].
   status input ststatus.
   del-yn = no.

   display
      rqc_desc
   with frame a.

   setloop1:
   do on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.

      set
         rqc_desc
         go-on("F5" "CTRL-D")
      with frame a.

      /*  DELETE  */
      if lastkey = keycode("F5") or lastkey = keycode("CTRL-D")
      then do:
         /* IS CATEGORY IN USE BY "ACTIVE" OR "FUTURE" APPROVERS? */
         if can-find(first rqa_mstr where
                           rqa_category = input rqc_category and
                           (rqa_end = ? or rqa_end >= today))
         then do:
            {pxmsg.i &MSGNUM=1848 &ERRORLEVEL=4} /* ACTIVE APPROVERS EXIST */
            undo, retry.
         end.

         del-yn = yes.
         /* CONFIRM DELETE */
         {pxmsg.i &MSGNUM=11 &ERRORLEVEL=1 &CONFIRM=del-yn}
         if del-yn = no then
            undo setloop1.
      end.  /* if lastkey = keycode("F5") */

      if del-yn
      then do:
         /* OK TO DELETE */
         for each rqcd_det exclusive-lock
               where rqcd_category = rqc_category:
            delete rqcd_det.
         end.

         delete rqc_mstr.
         clear frame a.
/*GUI*/ RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
         hide frame b.
         hide frame c.
         del-yn = no.
         next mainloop.
      end.  /* if del-yn */

   end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* setloop1  */

   setloop2:
   repeat:
/*GUI*/ if global-beam-me-up then undo, leave.

      do on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.


         rqcd_det_cntr = 0.
         clear frame b all no-pause.
         clear frame c all no-pause.
         view frame b.

         for each rqcd_det where rqcd_category = rqc_category
               no-lock:

            find ac_mstr where ac_code = rqcd_acct_from
               no-lock no-error.
            if available(ac_mstr) then
               var_acct_from_desc = ac_desc.
            else
               var_acct_from_desc = "".

            find ac_mstr where ac_code = rqcd_acct_to
               no-lock no-error.
            if available(ac_mstr) then
               var_acct_to_desc = ac_desc.
            else
               var_acct_to_desc = "".

            display
               rqcd_acct_from
               rqcd_sub_from
               var_acct_from_desc
               rqcd_acct_to
               rqcd_sub_to
               var_acct_to_desc
            with frame b.

            down with frame b width 80.

            rqcd_det_cntr = rqcd_det_cntr + 1.

            if rqcd_det_cntr > 7 then
               leave.
         end.  /* for each rqcd_det */

         prompt-for
            rqcd_acct_from
            rqcd_sub_from
            with frame c
         editing:

            /* FIND NEXT/PREVIOUS RECORD */
            {mfnp05.i rqcd_det rqcd_category
               "rqcd_category = rqc_category"
               rqcd_acct_from "input frame c rqcd_acct_from"}
            if recno <> ?
            then do:
               find ac_mstr where ac_code = rqcd_acct_from
                  no-lock no-error.
               if available(ac_mstr) then
                  var_acct_from_desc = ac_desc.
               else
                  var_acct_from_desc = "".

               find ac_mstr where ac_code = rqcd_acct_to
                  no-lock no-error.
               if available(ac_mstr) then
                  var_acct_to_desc = ac_desc.
               else
                  var_acct_to_desc = "".

               display
                  rqcd_acct_from
                  rqcd_sub_from
                  var_acct_from_desc
                  rqcd_acct_to
                  rqcd_sub_to
                  var_acct_to_desc
               with frame c.

            end.  /* if recno <> ? */

         end.  /* prompt-for */

         if input frame c rqcd_acct_from = ''
         then do:
            {pxmsg.i &MSGNUM=40 &ERRORLEVEL=3} /*BLANK NOT ALLOWED*/
            undo, retry.
         end.

         /*  USE var_acct_from AS TEMPORARY VARIABLE TO
         VALIDATE input rqcd_acct_from AND LATER
         VALIDATE input rqcd_acct_to RANGES.  */

         assign
            var_acct_from = input rqcd_acct_from
            var_sub_from = input rqcd_sub_from.

         find rqcd_det where
            rqcd_category = rqc_category
            and rqcd_acct_from = input frame c rqcd_acct_from
            and rqcd_sub_from  = input frame c rqcd_sub_from
            exclusive-lock no-error.

         if not available rqcd_det
         then do:

            /*  VALIDATE ACCOUNT */
            assign
               var_acct_from = input rqcd_acct_from
               var_sub_from = input rqcd_sub_from
               valid_acct = no.

            run validate_acct (input var_acct_from,
                               input var_sub_from,
                               output valid_acct).

            if not valid_acct
            then do:
               next-prompt rqcd_acct_from.
               undo, retry.
            end.

            /*  CHECK ACCOUNT OVERLAP */
            acct_overlap = no.

            do for work_rqcd_det:

               for each work_rqcd_det where
                     rqcd_category = rqc_mstr.rqc_category
                     no-lock:

                  /* CONVERT '?' ACCOUNT FOR COMPARISON */
                  if rqcd_acct_from = ?
                  then
                     work_rqcd_acct_from = "".
                  else
                     work_rqcd_acct_from = rqcd_acct_from.

                  if rqcd_sub_from = ?
                  then
                     work_rqcd_sub_from = "".
                  else
                     work_rqcd_sub_from = rqcd_sub_from.

                  if rqcd_acct_to = ?
                     or rqcd_acct_to = ""
                  then
                     work_rqcd_acct_to = hi_char.
                  else
                     work_rqcd_acct_to = rqcd_acct_to.

                  if rqcd_sub_to = ?
                     or rqcd_sub_to = ""
                  then
                     work_rqcd_sub_to = hi_char.
                  else
                     work_rqcd_sub_to = rqcd_sub_to.

                  /* IS INPUT ACCOUNT WITHIN EXISTING RANGE? */

                  if string(var_acct_from,"x(8)")
                     + var_sub_from
                     >= string(work_rqcd_acct_from,"x(8)")
                     + work_rqcd_sub_from
                     and string(var_acct_from,"x(8)")
                     + var_sub_from
                     <= string(work_rqcd_acct_to,"x(8)")
                     + work_rqcd_sub_to
                  then do:
                     acct_overlap = yes.
                     leave.
                  end.
               end.  /* for each work_rqcd_det */

            end.  /* do for work_rqcd_det */

            if acct_overlap
            then do:
               {pxmsg.i &MSGNUM=3142 &ERRORLEVEL=3}   /* Account Overlap */
               next-prompt rqcd_acct_from.
               undo, retry.
            end.

            {pxmsg.i &MSGNUM=1 &ERRORLEVEL=1}   /* Adding new record */

            create rqcd_det.

            assign
               rqcd_category  = input rqc_category
               rqcd_acct_from = input frame c rqcd_acct_from
               rqcd_sub_from  = input frame c rqcd_sub_from
               rqcd_acct_to   = input frame c rqcd_acct_from
               rqcd_sub_to    = input frame c rqcd_sub_from.

         end.  /* if not available rqcd_mstr */

         find ac_mstr where ac_code = rqcd_acct_from
            no-lock no-error.
         if available(ac_mstr) then
            var_acct_from_desc = ac_desc.
         else
            var_acct_from_desc = "".

         display
            rqcd_acct_from
            rqcd_sub_from
            var_acct_from_desc
         with frame c.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* do on error  */

      find ac_mstr where ac_code = rqcd_acct_to
         no-lock no-error.
      if available(ac_mstr) then
         var_acct_to_desc = ac_desc.
      else
         var_acct_to_desc = "".

      del-yn = no.
      ststatus = stline[2].
      status input ststatus.

      display
         rqcd_acct_to
         rqcd_sub_to
         var_acct_to_desc
      with frame c.

      setloop3:
      do on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.

         set
            rqcd_acct_to
            rqcd_sub_to
            go-on("F5" "CTRL-D") with frame c.

         find ac_mstr where ac_code = rqcd_acct_to
            no-lock no-error.
         if available(ac_mstr) then
            var_acct_to_desc = ac_desc.
         else
            var_acct_to_desc = "".

         display
            var_acct_to_desc
         with frame c.

         /* DELETE */
         if lastkey = keycode("F5") or
            lastkey = keycode("CTRL-D")
         then do:
            del-yn = yes.
            /* CONFIRM DELETE */
            {pxmsg.i &MSGNUM=11 &ERRORLEVEL=1 &CONFIRM=del-yn}
            if del-yn = no then
               undo setloop3.
         end.  /* if lastkey = keycode("F5") */

         if del-yn
         then do:
            delete rqcd_det.
            clear frame c.
            next setloop2.
         end.  /* if del-yn */

         /*  VALIDATE */
         if rqcd_acct_to = ''
         then do:
            {pxmsg.i &MSGNUM=40 &ERRORLEVEL=3} /*BLANK NOT ALLOWED*/
            next-prompt rqcd_acct_to.
            undo, retry.
         end.

         /*  USE var_acct_to AS TEMPORARY VARIABLE TO
         VALIDATE input rqcd_acct_to.  */
         if rqcd_acct_to = ""
         then
            assign
               var_acct_to = hi_char
               var_sub_to = hi_char
               valid_acct = yes.
         else do:
            assign
               var_acct_to = rqcd_acct_to
               var_sub_to = rqcd_sub_to
               valid_acct = no.

            run validate_acct (input var_acct_to,
                               input var_sub_to,
                               output valid_acct).

            if not valid_acct
            then do:
               next-prompt rqcd_acct_to.
               undo, retry.
            end.
         end.  /* if input rqcd_acct_to = "" */

         /*  CHECK ACCOUNT RANGE */

         if string(var_acct_from,"x(8)") + var_sub_from
            > string(var_acct_to,"x(8)") + var_sub_to
         then do:
            {pxmsg.i &MSGNUM=1784 &ERRORLEVEL=3}
            /* End Account Cannot Be Before Start Account */
            next-prompt rqcd_acct_to.
            undo, retry.
         end.

         /*  CHECK ACCOUNT OVERLAP */
         acct_overlap = no.
         do for work_rqcd_det:

            for each work_rqcd_det where
                  rqcd_category = rqc_mstr.rqc_category and
                  recid(work_rqcd_det) <> recid(rqcd_det)
                  no-lock:

               /* CONVERT '?' ACCOUNT FOR COMPARISON */
               if rqcd_acct_from = ?
               then
                  work_rqcd_acct_from = "".
               else
                  work_rqcd_acct_from = rqcd_acct_from.

               if rqcd_sub_from = ?
               then
                  work_rqcd_sub_from = "".
               else
                  work_rqcd_sub_from = rqcd_sub_from.

               if rqcd_acct_to = ?
                  or rqcd_acct_to = ""
               then
                  work_rqcd_acct_to = hi_char.
               else
                  work_rqcd_acct_to = rqcd_acct_to.

               if rqcd_sub_to = ?
                  or rqcd_sub_to = ""
               then
                  work_rqcd_sub_to = hi_char.
               else
                  work_rqcd_sub_to = rqcd_sub_to.

               /* IS INPUT ACCOUNT WITHIN EXISTING RANGE? */

               if string(var_acct_to,"x(8)")
                  + var_sub_to
                  >= string(work_rqcd_acct_from,"x(8)")
                  + work_rqcd_sub_from
                  and string(var_acct_to,"x(8)")
                  + var_sub_to
                  <= string(work_rqcd_acct_to,"x(8)")
                  + work_rqcd_sub_to
               then do:
                  acct_overlap = yes.
                  leave.
               end.

               /* IS INPUT RANGE NESTING AN EXISTING RANGE? */

               if string(var_acct_from,"x(8)")
                  + var_sub_from
                  <= string(work_rqcd_acct_from,"x(8)")
                  + work_rqcd_sub_from
                  and string(var_acct_to,"x(8)")
                  + var_sub_to
                  >= string(work_rqcd_acct_to,"x(8)")
                  + work_rqcd_sub_to
               then do:
                  acct_overlap = yes.
                  leave.
               end.

            end.  /* for each work_rqcd_det */

         end.  /* do for work_rqcd_det */

         if acct_overlap
         then do:
            {pxmsg.i &MSGNUM=3142 &ERRORLEVEL=3}   /* Account Overlap */
            next-prompt rqcd_acct_to.
            undo, retry.
         end.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* setloop3  */

   end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* setloop2  */

end.
/*GUI*/ if global-beam-me-up then undo, leave.
  /* mainloop repeat  */

status input.

/* NEW PROCEDURE TO VALIDATE ACCT/SUBACCT */

PROCEDURE validate_acct:

   define input  parameter acct       like glt_acct    no-undo.
   define input  parameter sub_acct   like glt_sub     no-undo.
   define output parameter valid_acct like mfc_logical no-undo.

   for first co_ctrl
      fields (co_pl co_use_sub)
      no-lock:
   end. /* FOR FIRST co_ctrl  */

   for first gl_ctrl
      fields (gl_verify)
      no-lock:
   end. /* FOR FIRST gl_ctrl */

   valid_acct = yes.

   if available gl_ctrl
      and gl_verify
   then do:

      /* CHECK IF ACCOUNT CODE IS AN ALLOCATION */
      for first al_mstr
         fields (al_code)
         where al_code = acct
         no-lock:
      end. /* FOR FIRST al_mstr */

      if available al_mstr
         and sub_acct <> ""
      then do:
         if not can-find(sb_mstr
                         where sb_sub = sub_acct)
         then do:
            /* INVALID SUB-ACCOUNT CODE */
            {pxmsg.i &MSGNUM=3131 &ERRORLEVEL=3}
            valid_acct = no.
            leave.
         end. /* IF NOT CAN-FIND sb_mstr */
         else do:
            /* SUB-ACCT WILL OVERRIDE ANY SUB-ACCT SPECIFIED */
            /* IN ALLOCATION                                 */
            {pxmsg.i &MSGNUM=3132 &ERRORLEVEL=2}
         end. /* ELSE DO */
      end. /* IF AVAILABLE al_mstr ... */

      /* NOT AN ALLOCATION CODE */
      if not available al_mstr
      then do:
         for first ac_mstr
            fields (ac_active ac_code ac_desc)
            where ac_code = acct
            and   ac_active
            no-lock:
         end. /* FOR FIRST ac_mstr */

         if not available ac_mstr
         or acct = co_pl
         then do:
            /* INVALID ACCOUNT CODE */
            {pxmsg.i &MSGNUM=3052 &ERRORLEVEL=3}
            valid_acct = no.
            leave.
         end. /* IF NOT AVAILABLE ac_mstr */

         if sub_acct <> ""
         then do:
            for first sb_mstr
               fields (sb_active sb_sub)
               where sb_sub = sub_acct
               no-lock:
            end. /* FOR FIRST sb_mstr */

            if not available sb_mstr
            then do:
               /* INVALID SUB-ACCOUNT CODE */
               {pxmsg.i &MSGNUM=3131 &ERRORLEVEL=3}
               valid_acct = no.
               leave.
            end. /* IF NOT AVAILABLE sb_mstr */
            else
            if not sb_active
            then do:
               /* INACTIVE SUB-ACCOUNT */
               {pxmsg.i &MSGNUM=3137 &ERRORLEVEL=3}
               valid_acct = no.
               leave.
            end. /* ELSE IF NOT sb_active */
         end. /* IF sub_acct <> "" */

         /* VALIDATE ACCT/SUB-ACCT COMBINATION */
         if  available co_ctrl
         and co_use_sub
         then do:
            for first cr_mstr
               fields (cr_code cr_code_beg cr_code_end cr_type)
               where cr_code    = sub_acct
               and cr_type      = "sub_acct"
               and cr_code_beg <= acct
               and cr_code_end >= acct
               no-lock:
            end. /* FOR FIRST cr_mstr */
            if not available cr_mstr
            then do:
               /* INVALID ACCT/SUB-ACCT/COST CENTER COMBINATION */
               {pxmsg.i &MSGNUM=3133 &ERRORLEVEL=3}
               valid_acct = no.
               leave.
            end. /* IF NOT AVAILABLE cr_mstr */
         end. /* IF AVAILABLE co_ctrl ... */
      end. /* IF NOT AVAILABLE al_mstr */
   end. /* IF AVAILABLE gl_ctrl AND gl_verify */

END PROCEDURE.
